package org.xx.armory.oss;

import java.util.Objects;

import static org.xx.armory.commons.Validators.rejectIfBlank;
import static org.xx.armory.commons.Validators.rejectIfNonPositive;

public class FileRef {
    private final String name;
    private final String key;
    private final long size;
    private final String etag;

    public FileRef(
            String name,
            String key,
            long size,
            String etag
    ) {
        this.name = rejectIfBlank(name, "name").strip();
        this.key = rejectIfBlank(key, "key").strip();
        this.size = rejectIfNonPositive(size, "size");
        this.etag = rejectIfBlank(etag, "eTag").strip().replace("\"", "");
    }

    public FileRef(
            String name,
            String key

    ) {
        this.name = rejectIfBlank(name, "name").strip();
        this.key = rejectIfBlank(key, "key").strip();
        this.size = 0L;
        this.etag = null;
    }

    /**
     * 获取文件名，不包含文件夹。
     *
     * @return 文件名。
     */
    public final String getName() {
        return this.name;
    }

    /**
     * 获取此文件在OSS系统中的key。
     *
     * @return 此文件在OSS系统中的key，不包含bucket。
     */
    public final String getKey() {
        return this.key;
    }

    public final long getSize() {
        return this.size;
    }

    public final String getEtag() {
        return this.etag;
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.name, this.key, this.size, this.etag);
    }

    @Override
    public boolean equals(
            Object obj
    ) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof FileRef)) {
            return false;
        }
        final var other = (FileRef) obj;
        return this.size == other.size && Objects.equals(this.name, other.name) && Objects.equals(this.key, other.key) && Objects.equals(this.etag, other.etag);
    }

    @Override
    public String toString() {
        return "FileRef("
                + "name=" + this.name + ","
                + "key=" + this.key + ","
                + "size=" + this.size + ","
                + "etag=" + this.etag
                + ")";
    }
}
